import { ref, reactive, onMounted, nextTick } from 'vue';
import { emitter } from "@/utils/mitt";
import { ElMessage, FormInstance } from "element-plus";
import { useRoute } from 'vue-router';
import {
  ProOBDDataGetApplyInsure,
  ProOBDDataDataAuth,
  ProOBDDataObtainEmp
} from "@/api/stu/InsuranceData";
import { SpliceDate } from "@/utils/public/method"
import { type PaginationProps } from "@pureadmin/table";
import qs from 'qs';
export function useODBEquipmentDataCenter() {
  const route = useRoute()
  const loading = ref(true);
  const dataList = ref([]);
  const btnloading = ref<boolean>(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 30,
    currentPage: 1,
    pageSizes: [5, 10, 30],
    background: true
  });
  const columns = [
    {
      label: "序号",
      type: "index",
      Width: 100
    },
    {
      label: "投保人",
      prop: "piApplicant",
    },
    {
      label: "保单号",
      prop: "piNumber",
    },
    {
      label: "申请时间",
      prop: "applyTime",
      cellRenderer: ({ row }) => (
        SpliceDate(row.applyTime)
      )
    }, {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];

  // 发起授权
  async function sendstatement(row) {
    let res = null;
    const data = {
      "taskId": route.query.taskId,
      "planId": route.query.planId,
      piApplicant: row.piApplicant,
      piNumber: row.piNumber,
      ppName: row.ppName
    }
    btnloading.value = true;
    if (row.getCar === 2) {
      res = await ProOBDDataObtainEmp(data)
    } else {
      res = await ProOBDDataDataAuth(data)
    }

    if (res.success) {
      ElMessage.success(res.msg)
      getDataList()
      btnloading.value = false;
    } else {
      ElMessage.error(res.msg)
      setTimeout(() => { btnloading.value = false; }, 1500)
    }
  }

  function handleSizeChange(val) {
    getDataList()
  }

  function handleCurrentChange(val) {
    getDataList()
  }
  async function getDataList() {
    const data = {
      "pageIndex": pagination.currentPage,
      "pageCount": pagination.pageSize,
      "taskId": route.query.taskId,
      "planId": route.query.planId,
    }
    const res = await ProOBDDataGetApplyInsure(data);
    if (res.success) {
      dataList.value = res.data?.items;
      pagination.total = res.data?.totalCount
      loading.value = false
    } else {
      ElMessage.error(res.msg)
      setTimeout(() => { loading.value = false }, 1500)
    }
  }
  onMounted(() => {
    getDataList()
  })
  return {
    loading,
    dataList,
    pagination,
    columns,
    btnloading,
    sendstatement,
    handleSizeChange,
    handleCurrentChange
  }
}
